package com.example.springmvcdemo1.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class TestController {

    @ResponseBody
    @RequestMapping("/feng")
    public String fengEr(){
        return "111";
    }

    // 请求转发 是服务器端帮用户实现的
    @RequestMapping("/fw1")
    public String myForward(){
        return "forward:/hello.html";
    }

    // 请求转发方式2
    @RequestMapping("/fw2")
    public void myForward2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/hello.html").forward(request, response);
    }

    // 请求重定向 1
    @RequestMapping("/rd")
    public String myRedirect() {
        return "redirect:/hello.html";
    }

    // 请求重定向 2
    @RequestMapping("/rd2")
    public void myRedirect2(HttpServletResponse response) throws IOException {
        response.sendRedirect("/hello.html");
    }
}
